package com.bh.weapp.controller.customer;

import com.bh.common.enums.OrderStatus;
import com.bh.common.core.controller.BaseController;
import com.bh.common.core.domain.AjaxResult;
import com.bh.common.core.page.TableDataInfo;
import com.bh.common.utils.StringUtils;
import com.bh.order.domain.JcLed;
import com.bh.order.dto.JcLedQueryDto;
import com.bh.order.service.IJcLedService;
import com.bh.order.vo.JcLedSignReceivingVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分段订单Controller
 *
 * @author slx
 * @date 2023-05-30
 */
@Api(tags = {"CUST分段订单Api"})
@RestController
@RequestMapping("/api/cust/led")
public class JcLedCustApiController extends BaseController {

    @Autowired
    private IJcLedService jcLedService;

    /**
     * 查询分段订单列表
     */
    @ApiOperation("查询分段订单列表")
    @PostMapping("/allList")
    public TableDataInfo allList(@RequestBody JcLedQueryDto queryVo) {
        startPage();
        List<JcLed> list = jcLedService.selectCustomerAllJcLedList(queryVo);
        return getDataTable(list);
    }


    /**
     * 查询分段订单列表
     */
    @ApiOperation("通过jcOrderId查询分段订单列表")
    @GetMapping("/customerList/{jcOrderId}")
    public AjaxResult customerList(@PathVariable Long jcOrderId) {
        JcLedQueryDto queryVo = new JcLedQueryDto();
        queryVo.setJcOrderId(jcOrderId);
        List<JcLed> list = jcLedService.selectCustomerJcLedList(queryVo);
        return AjaxResult.success(list);
    }

    /**
     * 修改分段订单
     */
    @ApiOperation("分段订单详情")
    @GetMapping("/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        JcLed jcLed = jcLedService.selectJcLedById(id);
        return AjaxResult.success(jcLed);
    }


    /**
     * 自提签收（签收的是分段）
     */
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("分段订单自提签收(暂不使用)")
    @PostMapping("/signReceiving")
    public AjaxResult signReceiving(@RequestBody JcLedSignReceivingVo signVo) {
        if (StringUtils.isEmpty(signVo.getLedId())) {
            return error("请选择订单签收！");
        }
        JcLed jcLed = jcLedService.selectJcLedById(signVo.getLedId());
        if (jcLed.getStatus() != OrderStatus.TRANSPORT_FINISHED.getValue() && jcLed.getStatus() != OrderStatus.READY_SEND.getValue()) {
            return error("订单未到达，禁止签收！");
        }
        signVo.setUserId(getUserId().toString());
        return toAjax(jcLedService.signReceiving(signVo));
    }
}